[experimental] Preserve CommonJS module boundaries - #14982
Draft
irvinebroque wants to merge 4 commits into
Draft
Conversation
🦋 Changeset detectedLatest commit: 37557d9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
UnknownError: ProviderInitError |
Contributor
|
@irvinebroque Bonk workflow failed. Check the logs for details. View workflow run · To retry, trigger Bonk again. |
Contributor
|
✅ All changesets look good |
@cloudflare/autoconfig
@cloudflare/build-output-utils
@cloudflare/config
create-cloudflare
@cloudflare/deploy-helpers
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-functions
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-auth
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Experimental draft
This PR is a prototype for review. It is not intended to merge.
Baseline reproduction: https://github.com/irvinebroque/canvaskit-module-registry-repro/
What changes for developers
Some npm packages still ship CommonJS. These packages can depend on CommonJS features such as
__dirname,__filename,require,module.exports, relativerequire()calls, and CommonJS cycles.Workers supports these features when it receives a file as a CommonJS module. Today, Wrangler and the Cloudflare Vite plugin can bundle that file into the Worker's ES module first. The runtime then receives an ES module, so CommonJS-only values such as
__dirnameare unavailable.This prototype preserves eligible npm CommonJS files when you enable the
new_module_registrycompatibility flag. It supports.jsand.cjsfiles in named npm packages when theirrequire()calls use string literals and resolve to CommonJS or JSON. You continue to import the package normally:You do not need a package allowlist, custom module rules, or package-specific source changes.
__dirname.__dirnamefailure and reaches its separate Wasm-loading path.Nothing changes unless you enable
new_module_registry.Wrangler
For bundled, module-format Workers, Wrangler now keeps statically reachable npm CommonJS files out of the main ES module. It includes those files as CommonJS modules when you run local development, a dry run, or a deployment.
Default imports continue to work from ES module Worker code. Named imports work when the package exposes them in a form the build can detect. Relative
require()calls, required JSON files, and CommonJS cycles retain their CommonJS behavior. Node.js built-ins continue to work when you enablenodejs_compat.This prototype does not change service-worker-format or
no_bundlebuilds.Cloudflare Vite plugin
The Vite plugin now preserves the same CommonJS boundaries in each Worker mode:
You use the same application imports in development, preview, and the build output used for deployment.
Scope and current limits
This behavior is experimental and requires
new_module_registry.require("package")andrequire("./file")calls with exactly one string-literal argument.require(variable), fail the build or development transform with an experimental diagnostic.require()that resolves to an ES module also fails the transform instead of producing an incomplete deployment.require("./data.json")returns the parsed value.__dirnamefailure. CanvasKit still needs a Workers-compatible Wasm loading setup, which is outside this PR.This behavior requires no workerd changes. The runtime already supplies the expected CommonJS context when tooling identifies a module as CommonJS.
Validation
The validation branch covers a CommonJS package that uses
__dirname,__filename, relativerequire(), JSON, a Node.js built-in, a cycle, and default and named ES module imports.I ran the harness with the prototype packages after updating two local expectations: required JSON becomes CommonJS code, and CanvasKit reaches a missing-Wasm-file error after
__dirnamesucceeds.The checks pass for:
__dirnamepath in Wrangler and Vite local developmentFocused test results:
@cloudflare/workers-utilsand@cloudflare/vite-pluginbuildsThe harness verifies deployable Wrangler and Vite build artifacts, but it does not perform a remote deployment.
Wrangler's runtime build also succeeds. In this checkout, unrelated implicit-
anyerrors inProxyController.tsstill block Wrangler declaration generation.